package com.example.administrator.caigou51.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.example.administrator.caigou51.EventBusClass.UpdateBlurImage;
import com.example.administrator.caigou51.EventBusClass.UpdateStoreManagerActivity;
import com.example.administrator.caigou51.R;
import com.example.administrator.caigou51.bean.CheckOrderBean;
import com.example.administrator.caigou51.global.App;
import com.example.administrator.caigou51.global.Constant;
import com.example.administrator.caigou51.recyclerCard.basic.MaterialListView;
import com.example.administrator.caigou51.recyclerCard.card.StoreManagerCard;
import com.example.administrator.caigou51.request.BaseResponse;
import com.example.administrator.caigou51.request.ICallBackForRequestServer;
import com.example.administrator.caigou51.request.RequestServerManager;

import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * Created by Administrator on 2016/9/5/005.
 */
public class StoreManagerActivity extends GBaseActivity {
    MaterialListView material_listview;
    private LinearLayout linearLayoutAddStore;
    private LinearLayout linearLayoutEmpty;
    private TextView textViewAddStore;
    public static final String TagOrderTotal= "TagOrderTotal";
    public static final String TagFromCheckOrder= "TagFromCheckOrder";
    public static final String TagFromUserCenter= "TagFromUserCenter";
    public static boolean isFromCheck =false;
    public static boolean isFromUserCenter =false;
    public static  String orderTotal="";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentViewFor720_1280(R.layout.store_manager_activity);
        findViewByIds();
        initDatas();
        TaskGetStoreData(false);
    }


    protected void findViewByIds() {
        material_listview= (MaterialListView) findViewById(R.id.material_listview);
        linearLayoutAddStore = (LinearLayout) findViewById(R.id.linearLayoutAddStore);
        linearLayoutEmpty = (LinearLayout) findViewById(R.id.linearLayoutEmpty);
        textViewAddStore= (TextView) findViewById(R.id.textViewAddStore);textViewAddStore.setOnClickListener(this);
    }


    protected void initDatas() {
        isFromCheck =false;
        isFromUserCenter=false;
        orderTotal=getIntent().getStringExtra(TagOrderTotal);
        setTopTitle("店铺管理");
        EventBus.getDefault().register(this);
        if (!TextUtils.isEmpty(getIntent().getStringExtra(TagFromCheckOrder)))
        {
            isFromCheck=true;
        }
        if (!TextUtils.isEmpty(getIntent().getStringExtra(TagFromUserCenter)))
        {
            isFromUserCenter=true;
        }
        //setRightText(View.VISIBLE,"删除");
        getBottomBarShowOtherActivity();
        material_listview.setVisibility(View.VISIBLE);
        linearLayoutAddStore.setVisibility(View.VISIBLE);
        linearLayoutEmpty.setVisibility(View.GONE);
    }

    protected void fillArrayInListView(List<CheckOrderBean.AddressEntity> addressEntityList) {

        material_listview.clear();
        for (int i=0;i<addressEntityList.size();i++)
        {
            StoreManagerCard storeManagerCard=new StoreManagerCard(this);
            storeManagerCard.setAddressEntity(addressEntityList.get(i));
            material_listview.add(storeManagerCard);

        }


    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId())
        {
            case R.id.textViewAddStore:
                Intent intent = new Intent(StoreManagerActivity.this,ZhiZhiCheckActivity.class);
                startActivity(intent);
                break;
        }
    }

    private void TaskGetStoreData(final boolean isloadmore) {
        RequestServerManager.getInstanceDIY().handleMethodDIY((GBaseActivity) getActivity(), null, isloadmore, null,
                Constant.getRootUrl() + Constant.Action.Action_StoreList, Constant.Action.Action_StoreList, RequestServerManager.getInstanceDIY().
                        getRequestParams(Constant.Action.Action_StoreList, App.getUserBean().getUserid(),App.getUserBean().getSession_id(),orderTotal), BaseResponse.class, new ICallBackForRequestServer<BaseResponse>() {
                    @Override
                    public void onSuccess(BaseResponse result) {
                        // JSONObject jsonObject = (JSONObject) result.getData();
                        List<CheckOrderBean.AddressEntity> storeBeanList = JSON.parseArray(result.getData().toString(), CheckOrderBean.AddressEntity.class);
                        fillArrayInListView(storeBeanList);
                       // Log.e(" result.getData()", result.getData().toString());
                    }

                    @Override
                    public void onFail(int type, String errorMsg, String json) {

                    }
                });
    }
    public void onEventMainThread(final UpdateStoreManagerActivity updateStoreManagerActivity) {
        TaskGetStoreData(false);
    }
    @Override
    protected void onDestroy() {
        isFromCheck =false;
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }
}